/**
 * 
 */
package org.mhz.filetodb.main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Mahan
 *
 * 
 */
public class SolrEntityCreator
{
    private static Connection mConnection = null;
    private static Statement mStatement = null;
    private static ResultSet mResultSet = null;
    
    public static void main(String[] args)
    {           
        StringBuilder stringBuilder = new StringBuilder();
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            mConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ootwdb", "root", "password");
            mStatement = mConnection.createStatement();           
                              
            String findTableNames = "SELECT node_id, node_content_location FROM menu_hierarchy_t where node_content_location is not null";
            
            mResultSet = mStatement.executeQuery(findTableNames); 
            
            while (mResultSet.next()) 
            {
                final String nodeId = mResultSet.getString("node_id");
                final String contentLocation = mResultSet.getString("node_content_location");
                
                stringBuilder.append("<entity name=\"");
                stringBuilder.append(nodeId);
                stringBuilder.append("\"\n");
                stringBuilder.append("        pk=\"id\"\n"); 
                stringBuilder.append("        query=\"select id,\n"); 
                stringBuilder.append("                      content,\n"); 
                stringBuilder.append("                      (SELECT node_id FROM menu_hierarchy_t where node_content_location = '");
                stringBuilder.append(contentLocation);
                stringBuilder.append("') as readableItem,\n");
                stringBuilder.append("                      (SELECT parent.node_name FROM menu_hierarchy_t parent, menu_hierarchy_t child where parent.node_id = child.node_parent_id and child.node_content_location = '");
                stringBuilder.append(contentLocation);
                stringBuilder.append("') as parent,\n");
                stringBuilder.append("                      (SELECT node_name FROM menu_hierarchy_t where node_content_location = '");
                stringBuilder.append(contentLocation);
                stringBuilder.append("') as nameOfReadableItem\n");
                stringBuilder.append("               FROM ");
                stringBuilder.append(contentLocation);
                stringBuilder.append("\n");
                stringBuilder.append("               WHERE '${dataimporter.request.clean}' != 'false' OR row_updated_dttm > '${dataimporter.last_index_time}'\" />\n");                
            }   
            
            System.out.println(stringBuilder);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (mResultSet != null)
                {
                    mResultSet.close();
                }
                
                if (mStatement != null)
                {
                    mStatement.close();
                }
                
                if (mConnection != null)
                {
                    mConnection.close();
                }                
            }
            catch (SQLException sqlEx)
            {
                sqlEx.printStackTrace();
            }             
        }     
    }
}
